package summer.hot100.v1;

import java.util.LinkedList;
import java.util.List;

public class Solution437 {
    /**
     * https://leetcode.cn/problems/find-all-anagrams-in-a-string/solutions/645290/438-zhao-dao-zi-fu-chuan-zhong-suo-you-z-nx6b
     *
     * @return
     */
    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) return 0;
        long res = withRoot(root, targetSum);
        int left = pathSum(root.left, targetSum);
        int right = pathSum(root.right, targetSum);
        return (int) (res + left + right);
    }

    public long withRoot(TreeNode root, long sum) {
        if (root == null) return 0;
        sum -= root.val;
        int res = sum == 0 ? 1 : 0;
        return res + withRoot(root.left, sum) + withRoot(root.right, sum);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
